###Space, Time, Space vs. Time#####
###02/01/2022######################
###Generate Tables##################

setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
#Get packages, attach them
pks=c("reshape2", "xtable")
needed=setdiff(pks,installed.packages())

if( length(needed) > 0){
  options(Ncpus=parallel::detectCores() %/% 1.25)
  install.packages(needed)
}

sapply(pks, library, character.only=T)
session = sessionInfo()

load("FINALphi_e_0.RData")


beta.results.static[5,] <- beta.results.static[5,]/tr
beta.results.sce[5,] <- beta.results.sce[5,]/tr
beta.results.sem[5,] <- beta.results.sem[5,]/tr 
beta.results.ldv[5,] <- beta.results.ldv[5,]/tr
beta.results.sar[5,] <- beta.results.sar[5,]/tr
beta.results.stadl[5,] <- beta.results.stadl[5,]/tr 


phi_e_0_Table <- as.matrix(rbind(cbind(beta.results.static[,1],beta.results.sce[,1],beta.results.sem[,1],beta.results.ldv[,1],beta.results.sar[,1],beta.results.stadl[,1]),
                                 cbind(beta.results.static[,3],beta.results.sce[,3],beta.results.sem[,3],beta.results.ldv[,3],beta.results.sar[,3],beta.results.stadl[,3]),
                                 cbind(beta.results.static[,5],beta.results.sce[,5],beta.results.sem[,5],beta.results.ldv[,5],beta.results.sar[,5],beta.results.stadl[,5]),
                                 cbind(beta.results.static[,7],beta.results.sce[,7],beta.results.sem[,7],beta.results.ldv[,7],beta.results.sar[,7],beta.results.stadl[,7])))

colnames(phi_e_0_Table) <- c("Static","SCE", "SEM", "LDV","SAR","STADL")
rownames(phi_e_0_Table) <- rep(c("Bias","Avg. SE","SD","RMSE","CP"),4)


print(xtable(phi_e_0_Table, type = "latex", digits=3), file = "phi_e_0_Table.tex")


load("FINALphi_e_0.1.RData")

beta.results.static[5,] <- beta.results.static[5,]/tr
beta.results.sce[5,] <- beta.results.sce[5,]/tr
beta.results.sem[5,] <- beta.results.sem[5,]/tr 
beta.results.ldv[5,] <- beta.results.ldv[5,]/tr
beta.results.sar[5,] <- beta.results.sar[5,]/tr
beta.results.stadl[5,] <- beta.results.stadl[5,]/tr 


phi_e_0.1_Table <- as.matrix(rbind(cbind(beta.results.static[,1],beta.results.sce[,1],beta.results.sem[,1],beta.results.ldv[,1],beta.results.sar[,1],beta.results.stadl[,1]),
                                   cbind(beta.results.static[,3],beta.results.sce[,3],beta.results.sem[,3],beta.results.ldv[,3],beta.results.sar[,3],beta.results.stadl[,3]),
                                   cbind(beta.results.static[,5],beta.results.sce[,5],beta.results.sem[,5],beta.results.ldv[,5],beta.results.sar[,5],beta.results.stadl[,5]),
                                   cbind(beta.results.static[,7],beta.results.sce[,7],beta.results.sem[,7],beta.results.ldv[,7],beta.results.sar[,7],beta.results.stadl[,7])))

colnames(phi_e_0.1_Table) <- c("Static","SCE", "SEM", "LDV","SAR","STADL")
rownames(phi_e_0.1_Table) <- rep(c("Bias","Avg. SE","SD","RMSE","CP"),4)


#print(xtable(phi_e_0.1_Table, type = "latex", digits=3), file = "phi_e_0.1_Table.tex")


load("FINALphi_e_0.2.RData")

beta.results.static[5,] <- beta.results.static[5,]/tr
beta.results.sce[5,] <- beta.results.sce[5,]/tr
beta.results.sem[5,] <- beta.results.sem[5,]/tr 
beta.results.ldv[5,] <- beta.results.ldv[5,]/tr
beta.results.sar[5,] <- beta.results.sar[5,]/tr
beta.results.stadl[5,] <- beta.results.stadl[5,]/tr 


phi_e_0.2_Table <- as.matrix(rbind(cbind(beta.results.static[,1],beta.results.sce[,1],beta.results.sem[,1],beta.results.ldv[,1],beta.results.sar[,1],beta.results.stadl[,1]),
                                   cbind(beta.results.static[,3],beta.results.sce[,3],beta.results.sem[,3],beta.results.ldv[,3],beta.results.sar[,3],beta.results.stadl[,3]),
                                   cbind(beta.results.static[,5],beta.results.sce[,5],beta.results.sem[,5],beta.results.ldv[,5],beta.results.sar[,5],beta.results.stadl[,5]),
                                   cbind(beta.results.static[,7],beta.results.sce[,7],beta.results.sem[,7],beta.results.ldv[,7],beta.results.sar[,7],beta.results.stadl[,7])))

colnames(phi_e_0.2_Table) <- c("Static","SCE", "SEM", "LDV","SAR","STADL")
rownames(phi_e_0.2_Table) <- rep(c("Bias","Avg. SE","SD","RMSE","CP"),4)


#print(xtable(phi_e_0.2_Table, type = "latex", digits=3), file = "phi_e_0.2_Table.tex")


load("FINALphi_e_0.3.RData")

beta.results.static[5,] <- beta.results.static[5,]/tr
beta.results.sce[5,] <- beta.results.sce[5,]/tr
beta.results.sem[5,] <- beta.results.sem[5,]/tr 
beta.results.ldv[5,] <- beta.results.ldv[5,]/tr
beta.results.sar[5,] <- beta.results.sar[5,]/tr
beta.results.stadl[5,] <- beta.results.stadl[5,]/tr 


phi_e_0.3_Table <- as.matrix(rbind(cbind(beta.results.static[,1],beta.results.sce[,1],beta.results.sem[,1],beta.results.ldv[,1],beta.results.sar[,1],beta.results.stadl[,1]),
                                   cbind(beta.results.static[,3],beta.results.sce[,3],beta.results.sem[,3],beta.results.ldv[,3],beta.results.sar[,3],beta.results.stadl[,3]),
                                   cbind(beta.results.static[,5],beta.results.sce[,5],beta.results.sem[,5],beta.results.ldv[,5],beta.results.sar[,5],beta.results.stadl[,5]),
                                   cbind(beta.results.static[,7],beta.results.sce[,7],beta.results.sem[,7],beta.results.ldv[,7],beta.results.sar[,7],beta.results.stadl[,7])))

colnames(phi_e_0.3_Table) <- c("Static","SCE", "SEM", "LDV","SAR","STADL")
rownames(phi_e_0.3_Table) <- rep(c("Bias","Avg. SE","SD","RMSE","CP"),4)


#print(xtable(phi_e_0.3_Table, type = "latex", digits=3), file = "phi_e_0.3_Table.tex")


load("FINALphi_e_0.4.RData")

beta.results.static[5,] <- beta.results.static[5,]/tr
beta.results.sce[5,] <- beta.results.sce[5,]/tr
beta.results.sem[5,] <- beta.results.sem[5,]/tr 
beta.results.ldv[5,] <- beta.results.ldv[5,]/tr
beta.results.sar[5,] <- beta.results.sar[5,]/tr
beta.results.stadl[5,] <- beta.results.stadl[5,]/tr 


phi_e_0.4_Table <- as.matrix(rbind(cbind(beta.results.static[,1],beta.results.sce[,1],beta.results.sem[,1],beta.results.ldv[,1],beta.results.sar[,1],beta.results.stadl[,1]),
                                   cbind(beta.results.static[,3],beta.results.sce[,3],beta.results.sem[,3],beta.results.ldv[,3],beta.results.sar[,3],beta.results.stadl[,3]),
                                   cbind(beta.results.static[,5],beta.results.sce[,5],beta.results.sem[,5],beta.results.ldv[,5],beta.results.sar[,5],beta.results.stadl[,5]),
                                   cbind(beta.results.static[,7],beta.results.sce[,7],beta.results.sem[,7],beta.results.ldv[,7],beta.results.sar[,7],beta.results.stadl[,7])))

colnames(phi_e_0.4_Table) <- c("Static","SCE", "SEM", "LDV","SAR","STADL")
rownames(phi_e_0.4_Table) <- rep(c("Bias","Avg. SE","SD","RMSE","CP"),4)


#print(xtable(phi_e_0.4_Table, type = "latex", digits=3), file = "phi_e_0.4_Table.tex")

load("FINALphi_e_0.5.RData")

beta.results.static[5,] <- beta.results.static[5,]/tr
beta.results.sce[5,] <- beta.results.sce[5,]/tr
beta.results.sem[5,] <- beta.results.sem[5,]/tr 
beta.results.ldv[5,] <- beta.results.ldv[5,]/tr
beta.results.sar[5,] <- beta.results.sar[5,]/tr
beta.results.stadl[5,] <- beta.results.stadl[5,]/tr 


phi_e_0.5_Table <- as.matrix(rbind(cbind(beta.results.static[,1],beta.results.sce[,1],beta.results.sem[,1],beta.results.ldv[,1],beta.results.sar[,1],beta.results.stadl[,1]),
                                   cbind(beta.results.static[,3],beta.results.sce[,3],beta.results.sem[,3],beta.results.ldv[,3],beta.results.sar[,3],beta.results.stadl[,3]),
                                   cbind(beta.results.static[,5],beta.results.sce[,5],beta.results.sem[,5],beta.results.ldv[,5],beta.results.sar[,5],beta.results.stadl[,5]),
                                   cbind(beta.results.static[,7],beta.results.sce[,7],beta.results.sem[,7],beta.results.ldv[,7],beta.results.sar[,7],beta.results.stadl[,7])))

colnames(phi_e_0.5_Table) <- c("Static","SCE", "SEM", "LDV","SAR","STADL")
rownames(phi_e_0.5_Table) <- rep(c("Bias","Avg. SE","SD","RMSE","CP"),4)


print(xtable(phi_e_0.5_Table, type = "latex", digits=3), file = "phi_e_0.5_Table.tex")
